package com.song.dao.impl;

import com.song.bean.Menu;
import com.song.bean.Role;
import com.song.bean.Users;
import com.song.dao.UsersDao;
import com.song.dao.Utils;

import javax.enterprise.inject.New;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UsersDaoImpl extends Utils implements UsersDao {


    /**
     * 用户登录
     */
    @Override
    public Users getUser(String userName, String passWord) {
        List list = new ArrayList<>();
        Users user = new Users();
        Role role = new Role();
        List<Menu> menuList = new ArrayList<>();


        try {

            list.add(userName);
            list.add(passWord);
            String sql = "SELECT u.realname,r.rolename,m.menuname,m.menuid,m.fathermenuid,m.url" +
                    " FROM users u,role r,menu m,middle" +
                    " WHERE u.roleid=r.roleid" +
                    " AND r.roleid=middle.roleid" +
                    " AND m.menuid=middle.menuid" +
                    " AND u.loginName=?" +
                    " AND u.password=?";
            resultSet = select(sql, list);
            while (resultSet.next()) {
                user.setRealname(resultSet.getString("realname"));
                //user.setUserid(resultSet.getInt("userid"));
                //user.setLoginName(resultSet.getString("loginName"));
                role.setRoleName(resultSet.getString("roleName"));

                Menu menu = new Menu();
                menu.setMenuId(resultSet.getInt("menuId"));
                menu.setMenuName(resultSet.getString("menuName"));
                menu.setFatherMenuId(resultSet.getInt("fathermenuid"));
                menu.setUrl(resultSet.getString("url"));
                menuList.add(menu);
            }
            role.setMenu(menuList);
            user.setRole(role);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return user;
    }

    /**
     * 查询用户信息（分页查询）
     *
     * @return 用户信息集合
     */
    @Override
    public List<Users> getUser(Integer index, Integer pagesize) {
        List<Users> usersList = new ArrayList<>();

        try {
            List list = new ArrayList<>();
            list.add((index - 1) * pagesize);
            list.add(pagesize);
            String sql = "select userid,loginName,realname,r.roleid,rolename from users u,role r where r.roleid=u.roleid limit ?,?";
            resultSet = select(sql, list);
            while (resultSet.next()) {
                Users user = new Users();
                user.setUserid(resultSet.getInt("userid"));
                user.setLoginName(resultSet.getString("loginName"));
                user.setRealname(resultSet.getString("realname"));
               /* user.setSex(resultSet.getInt("sex"));
                user.setRoleId(resultSet.getInt("roleid"));
                user.setEmail(resultSet.getString("email"));
                user.setPhone(resultSet.getString("phone"));
                user.setAddress(resultSet.getString("address"));
                user.setCardid(resultSet.getString("cardid"));
                user.setIntroduction(resultSet.getString("introduction"));*/
                Role role = new Role();
                role.setRoleId(resultSet.getInt("roleid"));
                role.setRoleName(resultSet.getString("rolename"));
                user.setRole(role);
                usersList.add(user);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return usersList;
    }

    /**
     * 获取总的信息数
     *
     * @return 信息数
     */
    @Override
    public Integer getTotalUser() {
        Integer total = 0;
        try {

            String sql = "SELECT count(*) FROM users u,role r WHERE u.roleid=r.roleid";
            resultSet = select(sql, null);
            while (resultSet.next()) {
                total = resultSet.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return total;

    }

    /**
     * 新增用户
     *
     * @param user
     * @return
     */
    @Override
    public Boolean insertUser(Users user) {
        try {
            String sql = "INSERT INTO users VALUES(null,?,?,?,?,?,?,?,?,?,?)";
            List<Object> list = new ArrayList<>();
            list.add(user.getLoginName());
            list.add(user.getPassword());
            list.add(user.getRealname());
            list.add(user.getSex());
            list.add(user.getRoleId());
            list.add(user.getEmail());
            list.add(user.getPhone());
            list.add(user.getAddress());
            list.add(user.getCardid());
            list.add(user.getIntroduction());
            int update = update(sql, list);
            if (update > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close();
        }
        return false;

    }

    /**
     * 通过用户ID找到用户信息
     *
     * @param id 用户的ID
     * @return
     */
    @Override
    public Users findById(Integer id) {
        Users user = new Users();
        try {
            String sql = "SELECT * from users where userid = ?";
            List<Object> list = new ArrayList<>();
            list.add(id);
            resultSet = select(sql, list);
            while (resultSet.next()) {
                user.setLoginName(resultSet.getString("loginName"));
                user.setRealname(resultSet.getString("realname"));
                user.setPassword(resultSet.getString("password"));
                user.setSex(resultSet.getInt("sex"));
                user.setRoleId(resultSet.getInt("roleid"));
                user.setEmail(resultSet.getString("email"));
                user.setPhone(resultSet.getString("phone"));
                user.setAddress(resultSet.getString("address"));
                user.setCardid(resultSet.getString("cardid"));
                user.setIntroduction(resultSet.getString("introduction"));
                user.setUserid(resultSet.getInt("userid"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return user;
    }

    /**
     * 修改用户信息
     *
     * @param user 被修改的用户
     * @return
     */
    @Override
    public Boolean updateUser(Users user) {
        try {
            String sql = "update users set loginName=?,password=?,realname=?,sex=?,roleid=?,email=?,phone=?,address=?,cardid=?,introduction=? where userid=?";
            List<Object> list = new ArrayList<>();
            list.add(user.getLoginName());
            list.add(user.getPassword());
            list.add(user.getRealname());
            list.add(user.getSex());
            list.add(user.getRoleId());
            list.add(user.getEmail());
            list.add(user.getPhone());
            list.add(user.getAddress());
            list.add(user.getCardid());
            list.add(user.getIntroduction());
            list.add(user.getUserid());
            int update = update(sql, list);
            if (update > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close();
        }
        return false;
    }
    /**
     * 删除用户
     * @param id
     * @return
     */
    @Override
    public Boolean deleteUser(Integer id) {
        try {
            String sql = "DELETE FROM users WHERE userid=?";
            List<Object> list = new ArrayList<>();
            list.add(id);
            int update = update(sql, list);
            if (update > 0){
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close();
        }
        return false;
    }



}
